package com.dzm.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzm.anno.RequirePermission;
import com.dzm.entity.Advertisement;
import com.dzm.service.AdvertisementService;
import com.dzm.utils.R;
import com.dzm.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者：Dzm
 * 时间：2024-08-31-9:43
 * 功能：
 */

@RestController
@RequestMapping("/api/adver")
public class AdverController {

    @Autowired
    private AdvertisementService advertisementService;

    @RequirePermission("adver:search")
    @PostMapping("/search/{pageNum}/{pageSize}")
    public R searchAdver(@PathVariable("pageNum") Integer pageNum,
                         @PathVariable("pageSize") Integer pageSize,
                         @RequestBody SearchVo searchVo) {

        Page<Advertisement> pageModel = advertisementService.searchAdver(pageNum, pageSize, searchVo);

        return R.ok().data("pageModel", pageModel);
    }

    @RequirePermission("adver:getAdverById")
    @GetMapping("/getAdverById/{id}")
    public R getById(@PathVariable("id") Integer id) {
        Advertisement advertisement = advertisementService.getById(id);

        return R.ok().data("adver", advertisement);
    }

    @RequirePermission("adver:saveAdver")
    @PostMapping("/saveAdver")
    public R saveAdver(@RequestBody Advertisement advertisement) {
        advertisementService.save(advertisement);

        return R.ok().message("添加成功！");
    }

    @RequirePermission("adver:updateAdver")
    @PutMapping("/updateAdver")
    public R updateAdver(@RequestBody Advertisement advertisement) {
        advertisementService.updateById(advertisement);

        return R.ok().message("修改成功！");
    }

    @RequirePermission("adver:removeAdvers")
    @DeleteMapping("/removeAdvers")
    public R removeAdvers(@RequestBody List<String> ids) {
        advertisementService.removeBatchByIds(ids);

        return R.ok().message("删除成功！");
    }
}
